package com.skyline.attraction.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.skyline.attraction.dao.AttractionFeatureDao;
import com.skyline.attraction.model.Attraction;
import com.skyline.attraction.model.AttractionFeature;
import com.skyline.attraction.service.AttractionFeatureService;
import com.skyline.base.exception.NoResourceException;
import com.skyline.base.type.Activity;
import com.skyline.base.type.Authority;
import com.skyline.common.bean.Page;
import com.skyline.user.model.User;
import com.skyline.wo.dao.AlbumDao;

@Service("attractionFeatureService")
public class AttractionFeatureServiceImpl implements AttractionFeatureService {

	@Autowired
	AttractionFeatureDao attractionFeatureDao;
	@Autowired
	AlbumDao albumDao;

	@Value("${album.cover.defaultCover}")
	private String cover;
	@Value("${album.cover.defaultExt}")
	private String ext;
	@Value("${attraction.feature.defaultPortrait}")
	private String defaultFeaturePortrait;

	@Override
	public AttractionFeature addFeature(AttractionFeature feature, Attraction attraction, User creator) {
		Long attractionId = attraction.getId();
		String attractionName = attraction.getName();
		String attractionPortrait = attraction.getPortrait();

		Long ownerId = attraction.getAdminId();
		String ownerNickname = attraction.getAdminNickname();
		String ownerPortrait = attraction.getAdminPortrait();

		Long creatorId = creator.getId();
		String creatorNickname = creator.getNickname();
		String creatorPortrait = creator.getPortrait();

		String title = feature.getTitle();

		Long albumId = albumDao.insertAlbumReturnId(attractionId, attractionName, attractionPortrait, title, "", title,
				Authority.PUBLIC, Activity.NORMAL, cover, ext);
		Long featureId = attractionFeatureDao.insertFeature(attractionId, attractionName, attractionPortrait, ownerId,
				ownerNickname, ownerPortrait, creatorId, creatorNickname, creatorPortrait, defaultFeaturePortrait, title, feature.getDetail(),
				feature.getConsume(), albumId);
		feature.setAlbumId(albumId);
		feature.setId(featureId);
		return feature;
	}

	@Override
	public void updateFeature(Long id, String title, String detail, Integer consume) {
		attractionFeatureDao.updateFeature(id, title, detail, consume);
	}

	@Override
	public void updateFeatureActivity(Long id, Activity activity) {
		attractionFeatureDao.updateActivity(id, activity);

	}

	@Override
	public void updateFeaturePortrait(Long id, String portrait) {
		attractionFeatureDao.updatePortrait(id, portrait);
	}

	@Override
	public AttractionFeature votes(Long id, Integer star) {
		AttractionFeature feature = getFeatureById(id);

		int votes = feature.getVotes();
		float oldStar = feature.getStar();
		int newVotes = votes +1;
		float newStar = (oldStar * votes + star) / (newVotes);
		
		attractionFeatureDao.updateStar(id, newStar);
		
		feature.setStar(newStar);
		feature.setVotes(newVotes);
		
		return feature;

	}

	@Override
	public List<AttractionFeature> listFeatureByAttractionId(Long attractionId, Page page) {
		return attractionFeatureDao.queryFeaturesByAttractionId(attractionId, page, Activity.NORMAL);
	}

	@Override
	public AttractionFeature getFeatureById(Long id) {
		AttractionFeature feature = attractionFeatureDao.queryFeatureById(id, Activity.NORMAL);
		if (feature == null) {
			throw new NoResourceException("不存在ID[" + id + "]的特色，可能该特色已被删除");
		}
		return feature;
	}

	@Override
	public List<AttractionFeature> listSomeFeatureOfAttraction(Long attractionId, Integer size) {
		return attractionFeatureDao.querySomeFeaturesOfAttraction(attractionId, Activity.NORMAL, size);
	}

}
